﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace SpaceArcade
{
    class Menu :Scene
    {
        
        private List<MenuItems> MenuItems;
        public Menu(Background bg, Form f)
            :base(bg, f)
        { }

        public int CurrentItem
        {
            get;
            private set;
        }

        

        public bool activeSaving = false;

        public Menu(Form f)
        {
            //this.Rocks = new List<Rock>();
            this.GameForm = f;
            this.gameBM = new Bitmap(GameForm.Width, GameForm.Height);
            //rand = new Random();
            this.Background = new Background("Background3.png", 800, 600);
            MenuItems = new List<MenuItems>();
            MenuItems.Add(new MenuItems("BtnNormal.png", "Btnhover.png", 50, 200));
            MenuItems.Add(new MenuItems("Loadnormal.png", "loadhover.png", 50, 270));
            if (!activeSaving)
            {
                MenuItems.Add(new MenuItems("Ratingnorm.png", "ratinghover.png", 50, 340));
                MenuItems.Add(new MenuItems("Exitnormal.png","Exit.png", 50, 410));
            }else
            {
                MenuItems.Add(new MenuItems("savenorm.png", "savehover.png", 50, 340));
                MenuItems.Add(new MenuItems("ratingnorm.png","ratinghover.png", 50, 410));
                MenuItems.Add(new MenuItems("exitnormal.png", "exit.png", 50, 480));
            }            
        }

        int currindex = -1;
        int previndex = 0;
        public void OnMove(object sender, MouseEventArgs e)
        {

            currindex = -1;
            for (int i = 0; i < MenuItems.Count; i++)
            {
                if ((e.X >= MenuItems[i].X) && (e.X < MenuItems[i].X + MenuItems[i].bm.Width) && (e.Y > MenuItems[i].Y) && (e.Y < MenuItems[i].Y + MenuItems[i].bm.Height))
                {
                    currindex = i;
                }
               
            }
            if (!activeSaving)
            {
                if (currindex != previndex)
                {
                    if (MenuItems.Count > 4)
                    {
                        MenuItems.RemoveAt(2);   
                        for (int i = 2; i < MenuItems.Count; i++)
                        {
                            MenuItems[i].Y -= 70;
                        }
                    }
                   
                   
                    previndex = currindex;
                    switch (currindex)
                    {
                        case 0:
                            MenuItems[currindex].SetActiveBM();
                            for (int i = 0; i < MenuItems.Count; i++)
                            {
                                if (i != currindex)
                                {
                                    MenuItems[i].SetNormalBM();
                                }
                            }
                            break;
                        case 1:
                            MenuItems[currindex].SetActiveBM();
                            for (int i = 0; i < MenuItems.Count; i++)
                            {
                                if (i != currindex)
                                {
                                    MenuItems[i].SetNormalBM();
                                }
                            }
                            break;
                        case 2:
                            MenuItems[currindex].SetActiveBM();
                            for (int i = 0; i < MenuItems.Count; i++)
                            {
                                if (i != currindex)
                                {
                                    MenuItems[i].SetNormalBM();
                                }
                            }
                            break;
                        case 3:
                            MenuItems[currindex].SetActiveBM();
                            for (int i = 0; i < MenuItems.Count; i++)
                            {
                                if (i != currindex)
                                {
                                    MenuItems[i].SetNormalBM();
                                }
                            }
                            break;
                        case -1:
                            for (int i = 0; i < MenuItems.Count; i++)
                            {
                                MenuItems[i].SetNormalBM();
                            }
                            break;
                    }
                }
            }else
            {
                if (MenuItems.Count < 5)
                {
                    MenuItems.Insert(2, new MenuItems("savenorm.png", "savehover.png", 50, 340));
                    for (int i = 3; i < MenuItems.Count; i++)
                    {
                        MenuItems[i].Y += 70;
                    }
                }
                    if (currindex != previndex)
                    {
                        previndex = currindex;
                        switch (currindex)
                        {
                            case 0:
                                MenuItems[currindex].SetActiveBM();
                                for (int i = 0; i < MenuItems.Count; i++)
                                {
                                    if (i != currindex)
                                    {
                                        MenuItems[i].SetNormalBM();
                                    }
                                }
                                break;
                            case 1:
                                MenuItems[currindex].SetActiveBM();
                                for (int i = 0; i < MenuItems.Count; i++)
                                {
                                    if (i != currindex)
                                    {
                                        MenuItems[i].SetNormalBM();
                                    }
                                }
                                break;
                            case 2:
                                MenuItems[currindex].SetActiveBM();
                                for (int i = 0; i < MenuItems.Count; i++)
                                {
                                    if (i != currindex)
                                    {
                                        MenuItems[i].SetNormalBM();
                                    }
                                }
                                break;
                            case 3:
                                MenuItems[currindex].SetActiveBM();
                                for (int i = 0; i < MenuItems.Count; i++)
                                {
                                    if (i != currindex)
                                    {
                                        MenuItems[i].SetNormalBM();
                                    }
                                }
                                break;
                            case 4:
                                MenuItems[currindex].SetActiveBM();
                                for (int i = 0; i < MenuItems.Count; i++)
                                {
                                    if (i != currindex)
                                    {
                                        MenuItems[i].SetNormalBM();
                                    }
                                }
                                break;
                            case -1:
                                for (int i = 0; i < MenuItems.Count; i++)
                                {
                                    MenuItems[i].SetNormalBM();
                                }
                                break;

                        }
                    }
            }           
        }

        public void OnClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < MenuItems.Count; i++)
            {
                if ((e.X >= MenuItems[i].X) && (e.X < MenuItems[i].X + MenuItems[i].bm.Width) && (e.Y > MenuItems[i].Y) && (e.Y < MenuItems[i].Y + MenuItems[i].bm.Height))
                {                   
                    CurrentItem = i;
                }               
            }
        }

        public override void Draw()
        {
            base.Draw();
            
            foreach(Sprite items in MenuItems)
            {
                items.Draw(gameBM);
            }
        }
    }
}
